package com.cloud.auth.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import com.cloud.auth.entity.Role;
import com.cloud.auth.entity.User;


//@Component
public class MyUserPrincipal  implements UserDetails{
	private User user;
	private Role role;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8210942565222122544L;
	/**
	 * 用户所拥有的权限
	 */
	private Collection<GrantedAuthority> authorities;
	
	public MyUserPrincipal() {
		
	}
	public MyUserPrincipal(User user,Role role) {
    	
        this.user = user;
        this.role=role;
    }
	
	public MyUserPrincipal(User user,Role role, Collection<GrantedAuthority> authorities) {
    	
        this.user = user;
        this.role=role;
        this.authorities=authorities;
    }

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		//此处注入角色集合或权限集合
		GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_" + role.getRoleName().toUpperCase());
//		authorities = new ArrayList<GrantedAuthority>();
		authorities.add(grantedAuthority);
		System.out.println(this.getUsername()+">>>>"+grantedAuthority.getAuthority());
		return authorities;
	}

	@Override
	public String getPassword() {
		// TODO Auto-generated method stub
		return this.user.getPwd();
	}

	@Override
	public String getUsername() {
		// TODO Auto-generated method stub
		return this.user.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}


}
